<template>
  <div class="addEmployee">
    <template>
      <el-dialog
        title="新增员工"
        :visible="showDialog"
        top="8vh"
        @close="clickClose"
        @click.native="handleDialogClick"
      >
        <!-- 表单 -->
        <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
          </el-form-item>
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
          </el-form-item>
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
          </el-form-item>
          <el-form-item label="聘用形式" prop="formOfEmployment">
            <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
              <el-option v-for="item in  hireType" :key="item.id"
              :label="item.value"
              :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="工号" prop="workNumber">
            <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
          </el-form-item>
          <el-form-item label="部门" prop="departmentName">
            <!-- input框的内容只能选，不能在框中修改 -->
            <!-- 设置input框的value属性，监听input事件，当输入框修改了，拿到新更新的值，更新到绑定的变量中 ,单项数据绑定-->
            <el-input
              :value=" formData.departmentName"
              style="width:50%"
              placeholder="请选择部门"
              @click.native.stop="handleClick"
            />
            <!-- 树形组件 -->
            <div v-if="showTree" class="tree-box">
              <!-- 滚动条 -->
              <el-scrollbar>
                <el-tree
                v-if="showTree"
                v-loading="isLoading"
                :data="treeData"
                :props="defaultProps"
                default-expand-all
                @node-click="handleNodeClick"
               />

              </el-scrollbar>
             
            </div>
          </el-form-item>
          <el-form-item label="转正时间">
            <!-- 日期控件 -->
            <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
          </el-form-item>
        </el-form>
        <!-- footer插槽 -->
        <template v-slot:footer>
          <el-button @click="clickClose">取消</el-button>
          <el-button type="primary" @click="submitAdd">确定</el-button>
        </template>
      </el-dialog>
    </template>
  </div>
</template>

<script>
import { reqShowDepartments } from '@/api/departments'
import { transListToTree } from '@/utils/index'
import { reqAddEmployee} from '@/api/employees'
import obj from '@/constant/employees'
const { hireType}=obj
export default {
  name: 'AddEmployee',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      treeData: [],
      defaultProps: {
        label: 'name'
      },
      showTree: false,
      isLoading: false,
      hireType:hireType,
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }

    }
  },
  methods: {
    // ** 关闭弹框
    clickClose() {
      this.$emit('update:showDialog', false)
      this.$refs.addForm.resetFildes()
      this.formData={
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
    },
    // ** [请选择部门]：列表式数据  depts ==> 树形结构数据
    // ** 给input框注册click事件，为了切换显示和隐藏，也使得获取的数据更具有实时性，也可以在打开弹框的时候发送请求
    async handleClick() {
      this.showTree = !this.showTree
      // 显示时只发一次请求 隐藏时不发请求
      if (this.showTree === false) return
      this.showTree = true
      this.isLoading = true
      const { data: { depts }} = await reqShowDepartments()
      const treeData = transListToTree(depts, '')
      this.treeData = treeData
      this.isLoading = false
    },
    handleDialogClick() {
      this.showTree = false
    },
    // ** 注册节点事件 ，给el-tree的每个节点都注册了 === 展开折叠操作
    handleNodeClick(data) {
      // 给子部门注册节点事件
      if (data.children && data.children.length > 0) return
      // console.log(111)
      // ==== 将选中的叶子节点[子部门]回显在input框中
      this.formData.departmentName = data.name
      this.showTree = false
    },
    // ** 点击确定 
    submitAdd(){
      this.$refs.addForm.validate( async(flag) => {
        if(!flag)return
        await reqAddEmployee(this.formData) 
        this.$message.success('添加成功')
        this.clickClose()
        // 通知到父组件更新数据
        this.$emit('add-employee')
        //  this.$parent.getEmployeeList() 找父组件，若遇到插槽，会被插槽影响 （不推荐）
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
    .el-scrollbar{
      height: 100%;
    }
    .el-scrollbar__wrap{
      // 横向溢出隐藏
      overflow-x: hidden;
    }
  }
}
</style>
